package com.meixiang.adapter.service;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.SelectionTime;
import com.meixiang.recyclerview.RecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 时间选择
 */
public class SelectionTimeAdapter extends RecyclerAdapter<SelectionTimeAdapter.ViewHolder> {
    private Context mContext;
    private List<SelectionTime> data=new ArrayList<>();
    public SelectionTimeAdapter() {

    }
    public void addData(List<SelectionTime> data){
        this.data.clear();
        this.data.addAll(data);
        notifyDataSetChanged();
    }
    public List<SelectionTime> getData(){
        return data;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext=parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_item_selection_time,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.tvTime.setText(data.get(position).getTime());
        holder.cbCheck.setChecked(data.get(position).isChecked());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (data.get(position).isChecked()){
                    data.get(position).setChecked(false);
                }else{
                    for (SelectionTime time:data){
                        time.setChecked(false);
                    }
                    data.get(position).setChecked(true);
                }
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.cb_check)
        CheckBox cbCheck;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
